package com.warehouse.controller;

import com.warehouse.common.Result;
import com.warehouse.entity.StockIn;
import com.warehouse.service.StockInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/stock-in")
@CrossOrigin(origins = "*")
public class StockInController {

    @Autowired
    private StockInService stockInService;

    @GetMapping
    public Result<List<StockIn>> getAllStockIns() {
        List<StockIn> stockIns = stockInService.getAllStockIns();
        return Result.success(stockIns);
    }

    @GetMapping("/{id}")
    public Result<StockIn> getStockInById(@PathVariable Long id) {
        StockIn stockIn = stockInService.getStockInById(id);
        if (stockIn != null) {
            return Result.success(stockIn);
        }
        return Result.error("入库记录不存在");
    }

    @GetMapping("/product/{productId}")
    public Result<List<StockIn>> getStockInsByProductId(@PathVariable Long productId) {
        List<StockIn> stockIns = stockInService.getStockInsByProductId(productId);
        return Result.success(stockIns);
    }

    @GetMapping("/date-range")
    public Result<List<StockIn>> getStockInsByDateRange(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        List<StockIn> stockIns = stockInService.getStockInsByDateRange(startTime, endTime);
        return Result.success(stockIns);
    }

    @GetMapping("/supplier")
    public Result<List<StockIn>> getStockInsBySupplier(@RequestParam String supplier) {
        List<StockIn> stockIns = stockInService.getStockInsBySupplier(supplier);
        return Result.success(stockIns);
    }

    @PostMapping
    public Result<StockIn> createStockIn(@RequestBody StockIn stockIn) {
        StockIn createdStockIn = stockInService.createStockIn(stockIn);
        return Result.success(createdStockIn);
    }

    @PostMapping("/import")
    public Result<Boolean> importFromExcel(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.error("请选择要上传的文件");
        }

        String fileName = file.getOriginalFilename();
        if (fileName == null || (!fileName.endsWith(".xlsx") && !fileName.endsWith(".xls"))) {
            return Result.error("请上传Excel文件(.xlsx或.xls)");
        }

        boolean success = stockInService.importFromExcel(file);
        if (success) {
            return Result.success(true);
        }
        return Result.error("导入失败，请检查文件格式");
    }

    @PutMapping("/{id}")
    public Result<Boolean> updateStockIn(@PathVariable Long id, @RequestBody StockIn stockIn) {
        stockIn.setId(id);
        boolean success = stockInService.updateStockIn(stockIn);
        if (success) {
            return Result.success(true);
        }
        return Result.error("更新入库记录失败");
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> deleteStockIn(@PathVariable Long id) {
        boolean success = stockInService.deleteStockIn(id);
        if (success) {
            return Result.success(true);
        }
        return Result.error("删除入库记录失败");
    }
} 